package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.common.UserQuery;
import org.chen.common.UserWithChat;
import org.chen.exception.CustomException;
import org.chen.mapper.UserQueryMapper;
import org.chen.result.BaseResponse;
import org.chen.result.CommonResponse;
import org.chen.service.UserQueryService;
import org.chen.service_enum.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class UserQueryServiceImpl extends ServiceImpl<UserQueryMapper, UserQuery> implements UserQueryService {
    @Autowired
    private UserQueryMapper userQueryMapper;

    @Override
    public BaseResponse<List<UserQuery>> loadDates(UserQuery userQuery) {
        if (userQuery == null){
            throw new CustomException(400,"参数错误");
        }
        List<UserQuery> userId = userQueryMapper.selectList(new QueryWrapper<UserQuery>().eq("userId", userQuery.getUserId()));
        if (userId == null){
            return BaseResponse.error(ErrorCode.USER_QUERY_NOT_FOUND.getCode(),ErrorCode.USER_QUERY_NOT_FOUND.getMessage(),ErrorCode.USER_QUERY_NOT_FOUND.getMessage());
        }
        return BaseResponse.success(userId);
    }

    @Override
    public CommonResponse saveUserQuery(UserQuery userQuery) {
        if (userQuery == null){
            throw new CustomException(400,"参数错误");
        }
        int insert = userQueryMapper.insert(userQuery);
        if (insert > 0){
            return CommonResponse.success();
        }
        return CommonResponse.error(ErrorCode.SAVE_FAILED.getCode(),ErrorCode.SAVE_FAILED.getMessage(),ErrorCode.SAVE_FAILED.getMessage());
    }

    @Override
    public BaseResponse<List<UserQuery>> loadDatesByDate(UserQuery userQuery) {
        if (userQuery == null){
            throw new CustomException(400,"参数错误");
        }
        List<UserQuery> userId = userQueryMapper.selectList(new QueryWrapper<UserQuery>().eq("userId", userQuery.getUserId()).eq("date",userQuery.getDate()));
        if (userId == null){
            return BaseResponse.error(ErrorCode.USER_QUERY_NOT_FOUND.getCode(),ErrorCode.USER_QUERY_NOT_FOUND.getMessage(),ErrorCode.USER_QUERY_NOT_FOUND.getMessage());
        }
        return BaseResponse.success(userId);
    }
}
